<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //字面量
        var obj = {}

        //内置构造函数
        var obj1 = new Object();
        obj1.name = "大明"

        //工厂函数
        function Factory(name, age) {
            var obj2 = {};
            this.name = name;
            this.age = age;
            return obj2;
        }
        var obj2 = Factory("张三", 20);
        console.log(obj2);


        //自定义构造函数

        function Person(name, age) {
            this.name = name;
            this.age = age;
            return {
                name: "钟鸣"
            }
        }

        //构造函数原型添加方法
        Person.prototype.fn = function () {
            console.log("fn...");
        }
        var p1 = new Person("张三", 40);
        console.log(p1);




        /* 手写new */
        function myNew(constr, ...argv) {
            var obj = {};
            const re = constr.call(obj, ...argv);
            obj.__proto__ = constr.prototype;
            if (Object.prototype.toString.call(re) === "[object Object]" || Object.prototype.toString.call(re) ===
                "[object Function]") {
                return re;
            }
            return obj;
        }
        const p2 = myNew(Person, "老六", 18);
        console.log(p2);
    </script>
</body>

</html>